package com.quan.mall.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.commons.core.bean.R;
import com.quan.commons.core.biz.support.MyBaseController;
import com.quan.commons.core.utils.*;
import com.quan.mall.commons.vo.PmsAttrAttrgroupRelationVo;
import com.quan.mall.commons.vo.PmsAttrVo;
import com.quan.mall.entity.PmsAttr;
import com.quan.mall.service.PmsAttrAttrgroupRelationService;
import com.quan.mall.service.PmsAttrService;
import com.quan.mall.service.PmsCategoryService;
import com.quan.system.commons.vo.SysUserVo;
import com.quan.commons.log.annotation.SysLog;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

import com.quan.mall.commons.vo.PmsAttrGroupVo;
import com.quan.mall.entity.PmsAttrGroup;
import com.quan.mall.service.PmsAttrGroupService;


/**
 * 商品-属性分组
 *
 * @author yhaoquan
 * @email yhaoquan@163.com
 * @date 2021-02-09 11:44:02
 */
@Slf4j
@Api(tags = "商品-属性分组")
@RestController
@RequestMapping("/mall/pmsattrgroup")
public class PmsAttrGroupController extends MyBaseController {

    @Autowired
    private PmsAttrGroupService service;

    @Autowired
    private PmsAttrService pmsAttrService;

    @Autowired
    private PmsCategoryService pmsCategoryService;

    @Autowired
    private PmsAttrAttrgroupRelationService pmsAttrAttrgroupRelationService;

    /**
     * 保存
     * @param vo
     * @return
     */
    @SysLog("保存")
    @PostMapping("save")
    public R save(@Valid @RequestBody PmsAttrGroupVo vo) {
        this.service.save(vo);
        return R.ok();
    }

    /**
     * 修改
     * @param vo
     * @return
     */
    @SysLog("修改")
    @PostMapping("update")
    public R update(@Valid @RequestBody PmsAttrGroupVo vo) {
        this.service.updateById(vo);
        return R.ok();
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @SysLog("删除")
    @PostMapping("delete")
    public R delete(@RequestBody Long[] ids) {
        if (null != ids && ids.length > 0) {
            this.service.removeByIds(Arrays.asList(ids));
            return R.ok();
        } else {
            return R.failure();
        }
    }

    /**
     * 多条件查询信息详情
     * @param vo
     * @return
     */
    @SysLog("查询详情")
    @GetMapping("/info")
    public R info(PmsAttrGroupVo vo) {
        QueryWrapper<PmsAttrGroup> queryWrapper = new QueryWrapper<PmsAttrGroup>(vo);
        PmsAttrGroup pmsAttrGroup = this.service.getOne(queryWrapper);

        PmsAttrGroupVo attrGroupVo = new PmsAttrGroupVo();
        BeanUtils.copyProperties(pmsAttrGroup, attrGroupVo);

        // 根据分类ID获取父分类ID路径
        Long[] path = this.pmsCategoryService.findCategoryPath(pmsAttrGroup.getCateId());
        attrGroupVo.setCategoryPath(path);

        return R.ok().put("data", attrGroupVo);
    }

    /**
     * 根据ID查询信息详情
     * @param {id}
     * @return
     */
    @SysLog("查询详情")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        PmsAttrGroup attrGroup = this.service.getById(id);

        PmsAttrGroupVo attrGroupVo = new PmsAttrGroupVo();
        BeanUtils.copyProperties(attrGroup, attrGroupVo);

        // 根据分类ID获取父分类ID路径
        Long[] path = this.pmsCategoryService.findCategoryPath(attrGroup.getCateId());
        attrGroupVo.setCategoryPath(path);

        return R.ok().put("data", attrGroupVo);
    }

    /**
     * 列表查询
     * @param vo
     * @return
     */
    @SysLog("查询列表")
    @GetMapping(value = "list")
    public Object list(PmsAttrGroupVo vo) {
        List<PmsAttrGroupVo> list = this.service.queryList(new PageUtils(request), vo);
        return R.ok().put("data", list);
    }

    /**
     * 分页查询
     * @param vo
     * @return
     */
    @SysLog("分页查询")
    @GetMapping(value = "page")
    public R queryPage(PmsAttrGroupVo vo) {
        PageUtils page = this.service.queryPage(new PageUtils(request), vo);
        return R.ok().put("data", page);
    }

    /**
     * 分页查询分组
     * @param entity
     * @return
     */
    @GetMapping(value = "queryAttrGroupPage")
    public R queryAttrGroupPage(PmsAttrGroup entity) {
        final PageUtils pageUtils = this.service.queryAttrGroupPage(new PageUtils(request), entity);
        return R.ok().put("data", pageUtils);
    }

    /**
     * 获取属性分组有关联的属性
     * @param groupId
     * @return
     */
    @GetMapping(value = "/getGroupAttrs/{groupId}")
    public R queryAttrGroupPage(@PathVariable("groupId") Long groupId) {
        final List<PmsAttr> groupAttrs = this.pmsAttrService.getGroupAttrs(groupId);
        return R.ok().put("data", groupAttrs);
    }

    /**
     * 获取属性分组没有关联的其他属性
     * @param groupId
     * @param vo
     * @return
     */
    @GetMapping(value = "/{groupId}/noattr/relation")
    public R attrNoattrRelation(@PathVariable("groupId") Long groupId, PmsAttrVo vo) {

        PageUtils page = pmsAttrService.getNoRelationAttr(new PageUtils(request), groupId, vo);

        return R.ok().put("data", page);
    }

    /**
     * 删除属性分组关联
     * @param vos
     * @return
     */
    @PostMapping(value = "/attr/relation/delete")
    public R deleteRelation(@RequestBody PmsAttrGroupVo[] vos) {

        pmsAttrService.deleteRelation(vos);

        return R.ok();
    }

    /**
     * 保存属性组关联属性
     * @param vos
     * @return
     */
    @PostMapping(value = "/attr/relation")
    public R addRelation(@RequestBody List<PmsAttrGroupVo> vos) {
        this.pmsAttrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     * @param cateId
     * @return
     */
    @GetMapping(value = "/{cateId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("cateId") Long cateId) {

        //1、查出当前分类下的所有属性分组
        //2、查出每个属性分组下的所有属性
        List<PmsAttrGroupVo> vos = this.service.getAttrGroupWithAttrsByCatelogId(cateId);

        return R.ok().put("data",vos);
    }

}
